<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Option extends CI_Controller {

    public $user_id;
    
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('expenses');
        $this->load->model('users');
        $this->load->helper('url');
        $this->user_id = $this->session->userdata('user_id');
    }
    
    public function check(){
        if($this->session->userdata('logged') == null){
            redirect('user/login');
        }
    }
    
    public function index(){
        $this->check();
        $data = array();
        $data['title'] = 'Option';
        $item = array();
        $item['cat'] = $this->expenses->get_category();
        $item['user'] = $this->users->get_users('user_id',$this->user_id)->first_row();
        $data['content'] = $this->load->view('option/option',$item,TRUE);
        $this->load->view('main',$data);
    }
    
    public function add_cat(){
        $this->check();
        $title = trim($this->input->post('newCat'));
        if($title != ''){
            $this->expenses->insert_category($title);
            $this->session->set_flashdata('option_status', 'Category has been added');
        }
        redirect('option');
    }
    
    public function edit_cat($cat_id){
        $this->check();
        $data = array();
        $data['title'] = 'Option';
        $item = array();
        $cat = $this->expenses->get_category_by_id($cat_id);
        if($cat->user_id == $this->user_id){
            $item['title'] = $cat->category_title;
            $item['id'] = $cat->category_id;
            $data['content'] = $this->load->view('option/edit_cat',$item,TRUE);
            $this->load->view('main',$data);
        } else {
            $this->session->set_flashdata('option_status', 'You dont have permission to edit this category');
            redirect('option');
        }
    }
    
    public function edit_cat_post($cat_id){
        $this->check();
        $cat = $this->expenses->get_category_by_id($cat_id);
        if($cat->user_id == $this->user_id){
            $title = $this->input->post('newCat');
            $this->expenses->update_category($cat_id,$title);
            $this->session->set_flashdata('option_status', 'Category edited');
            redirect('option');
        } else {
            $this->session->set_flashdata('option_status', 'You dont have permission to edit this category');
            redirect('option');
        }
    }
    
    public function delete_cat($cat_id){
        $this->check();
        $user_id = $this->expenses->get_category_owner($cat_id);
        if($user_id == $this->user_id){
            $this->expenses->delete_category($cat_id);
            $this->session->set_flashdata('option_status', 'Category has been deleted');
        } else {
            $this->session->set_flashdata('option_status', 'You dont have permission to delete this category');
        }
        redirect('option');
    }
    
    public function edit_name(){
        $this->check();
        $item = array();
        $data = array();
        $data['title'] = 'Option';
        $item['user'] = $this->users->get_users('user_id',$this->user_id)->first_row();
        $data['content'] = $this->load->view('option/edit_name',$item,TRUE);
        $this->load->view('main',$data);    
    }
    
    public function edit_name_post(){
        $this->check();
        $name = trim($this->input->post('newName'));
        $this->users->update_name($this->user_id,$name);
        $this->session->set_flashdata('option_status', 'Your name has been changed');
        redirect('option');
    }
    
    public function edit_password(){
        $this->check();
        $item = array();
        $data = array();
        $data['title'] = 'Option';
        $item['user'] = $this->users->get_users('user_id',$this->user_id)->first_row();
        $data['content'] = $this->load->view('option/edit_password',$item,TRUE);
        $this->load->view('main',$data);    
    }
    
    public function edit_password_post(){
        $this->check();
        $old = md5(trim($this->input->post('oldPass')));
        $new = trim($this->input->post('newPass'));
        $newC = trim($this->input->post('newPassc'));
        $user = $this->users->get_users('user_id',$this->user_id)->first_row();
        if (strcmp($user->user_pwd,$old) != 0){
            $this->session->set_flashdata('option_status', 'Your old password doesn\'t match');
            redirect('option/edit_password');
        } else if(strcmp($new,'') == 0){
            $this->session->set_flashdata('option_status', 'New password can\'t be blank');
            redirect('option/edit_password');
        } else if(strlen($new) < 6){
            $this->session->set_flashdata('option_status', 'New password can\'t be less than 6 character');
            redirect('option/edit_password');
        } else if(strcmp($new,$newC) != 0){
            $this->session->set_flashdata('option_status', 'New password doesn\'t match');
            redirect('option/edit_password');
        } else {
            $this->users->update_password($this->user_id,md5($new));
            $this->session->set_flashdata('option_status', 'Your password has been changed');
            redirect('option');
        }
    }
}

?>